package com.jye.rseven.user.controller;


import com.jye.rseven.domain.Sharing;
import com.jye.rseven.service.SharingService;
import com.jye.rseven.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sharing")
@Api(tags = "推广控制器API")
public class SharingController {

    @Autowired
    SharingService sharingService;


    /*
     * 通过用户id查找推广
     * */
    @GetMapping("/findSharingById")
    @ApiOperation(value = "查找推广", notes = "通过用户id查找推广", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findSharingById(int uId) {
        JsonResult jsonResult = new JsonResult();
        Sharing s = sharingService.findSharingByuId(uId);
        if (!StringUtils.isEmpty(s)) {
            jsonResult.setData(s);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
     * 查找推广
     * */
    @GetMapping("/findAllSharing")
    @ApiOperation(value = "查找所有推广", notes = "查找所有推广", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findAllSharing(int skip, int limit) {
        JsonResult jsonResult = new JsonResult();
        List<Sharing> list = sharingService.findAllSharing(skip, limit);
        if (list != null) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
     * 生成推广数据
     * */
    @PostMapping("/addSharing")
    @ApiOperation(value = "生成推广数据", notes = "通过cId和uId生成推广数据", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "sharing",
                    value = "推广实体类", required = true,
                    dataType = "com.jye.rseven.user.domain.Sharing"),
    })
    public JsonResult addSharing(Sharing sharing) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(sharingService.addSharing(sharing));
        return jsonResult;
    }

    /*
     * 修改推广记录
     * */
    @PutMapping("/updateSharing")
    @ApiOperation(value = "修改推广记录", notes = "修改推广记录,只能修改分享码", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "sharing",
                    value = "推广实体类", required = true,
                    dataType = "com.jye.rseven.user.domain.Sharing"),
    })
    public JsonResult updateSharing(Sharing sharing) {
        JsonResult jsonResult = new JsonResult();
        System.out.println(sharing);
        int i = sharingService.updateSharing(sharing);
        if (i == 1) {
            jsonResult.setMsg("修改成功!");
        } else {
            jsonResult.setMsg("修改失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }


    /*
     * 通过分享码查询
     * */
    @GetMapping("/findSharingByShareCode")
    @ApiOperation(value = "查找推广", notes = "通过分享码shareCode查找推广", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "shareCode",
                    value = "分享码", required = true,
                    dataType = "String"),
    })
    public JsonResult findChargeRecordByShareCode(String shareCode) {
        JsonResult jsonResult = new JsonResult();
        Sharing sharing = sharingService.findSharingByShareCode(shareCode);
        if(!StringUtils.isEmpty(sharing)){
            jsonResult.setData(sharing);
        }else {
            jsonResult.setMsg("未查找到该推广记录!请确认分享码是否正确!");
        }
        return jsonResult;
    }
    /*
     * 通过用户id和课程id查找分享码
     * */
    @GetMapping("/findShareCodeByuIdAndCId")
    @ApiOperation(value = "查找分享码", notes = "通过用户id和课程id查找分享码", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "cId",
                    value = "课程ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findShareCodeById(int uId,int cId) {
        JsonResult jsonResult = new JsonResult();
        String s = sharingService.findShareCodeByuIdAndCId(uId,cId);
        if (!StringUtils.isEmpty(s)) {
            jsonResult.setData(s);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }
}
